package unilearn.da;

import java.sql.*;
import java.util.Vector;

import javax.naming.*;

import unilearn.domain.AssessmentCriteria;

/**
 * @author Yin Zhang, 5393558
 * @author Ben Morrall, 4158768
 * @version 0.1, 04 May 2008, initial creation
 * @version 0.2, 14 May 2008, Code Refractor, blm
 */
public class DAAssessmentCriteria extends DABase
{

	public static Vector<AssessmentCriteria> getCriteriaByAssignment(long assignmentID)
		throws SQLException, NamingException
	{
		Vector<AssessmentCriteria> criterias = new Vector<AssessmentCriteria>();
		PreparedStatement ps = null;
		try
		{
			Connection conn = getConn();

			ps = conn.prepareStatement("SELECT * FROM assessment_criteria WHERE assignment_ID = ? order by display_order");
			ps.setLong(1, assignmentID);

			ResultSet rs = ps.executeQuery();

			while (rs.next())
			{
				AssessmentCriteria criteria = new AssessmentCriteria(
						rs.getLong("assessment_criteria_id"));
				criteria.setCriteriaName(rs.getString("name"));
				criteria.setCriteriaDescription(rs.getString("description"));
				criteria.setCriteriaMaxGrade(rs.getShort("max_grade"));
				criteria.setCriteriaDisplayOrder(rs.getShort("display_order"));
				criteria.setAssignmentID(rs.getLong("assignment_id"));

				criterias.add(criteria);
			}
		}
		finally
		{
			if (ps != null) ps.close();
		}

		return criterias;
	}

	public static AssessmentCriteria getCriteriaByID(long criteriaID) throws SQLException,
		NamingException
	{
		AssessmentCriteria criteria = null;
		PreparedStatement ps = null;

		try
		{
			Connection conn = getConn();

			ps = conn.prepareStatement("SELECT * FROM assessment_criteria WHERE assessment_criteria_id = ?");
			ps.setLong(1, criteriaID);

			ResultSet rs = ps.executeQuery();

			if (rs.next())
			{
				criteria = new AssessmentCriteria(rs.getLong("assessment_criteria_id"));
				criteria.setCriteriaName(rs.getString("name"));
				criteria.setCriteriaDescription(rs.getString("description"));
				criteria.setCriteriaMaxGrade(rs.getShort("max_grade"));
				criteria.setCriteriaDisplayOrder(rs.getShort("display_order"));
				criteria.setAssignmentID(rs.getLong("assignment_id"));
			}
		}
		finally
		{
			if (ps != null) ps.close();
		}

		return criteria;
	}

	public static void insertAssessmentCriteria(AssessmentCriteria criteria)
		throws SQLException, NamingException
	{
		PreparedStatement ps = null;

		try
		{
			Connection conn = getConn();

			ps = conn.prepareStatement("INSERT INTO assessment_criteria"
					+ "(name, description, display_order, max_grade, assignment_id) "
					+ "VALUES (?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS);

			ps.setString(1, criteria.getCriteriaName());
			ps.setString(2, criteria.getCriteriaDescription());
			ps.setInt(3, criteria.getCriteriaDisplayOrder());
			ps.setInt(4, criteria.getCriteriaMaxGrade());
			ps.setLong(5, criteria.getAssignmentID());

			ps.execute();
		}
		finally
		{
			if (ps != null) ps.close();
		}
	}

	public static void updateCriteria(long criteriaID, AssessmentCriteria criteria)
		throws SQLException, NamingException
	{
		PreparedStatement ps = null;

		try
		{
			Connection conn = getConn();

			ps = conn.prepareStatement("UPDATE assessment_criteria SET "
					+ "name = ?, description = ?, "
					+ "display_order = ?, max_grade = ?, assignment_id = ? "
					+ "WHERE assessment_criteria_id = ?");

			ps.setString(1, criteria.getCriteriaName());
			ps.setString(2, criteria.getCriteriaDescription());
			ps.setInt(3, criteria.getCriteriaDisplayOrder());
			ps.setInt(4, criteria.getCriteriaMaxGrade());
			ps.setLong(5, criteria.getAssignmentID());
			ps.setLong(6, criteriaID);

			ps.execute();

		}
		finally
		{
			if (ps != null) ps.close();
		}
	}

	/**
	 * Prevent Instances of DAAssessmentCriteria
	 */
	private DAAssessmentCriteria()
	{
	}
}